package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService cartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        //1、调用service
        ShoppingCart sc = cartService.add(shoppingCart);
        //2、返回结果
        return R.success(sc);
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        return R.success(cartService.sub(shoppingCart));
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //1、调用service
        List<ShoppingCart> list = cartService.list();
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        cartService.clean();
        return R.success("清空购物车成功");
    }
}
